package com.yc.navy.goods.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yc.navy.goods.bean.AddressInfo;
import com.yc.navy.goods.dto.MemberInfoDTO;
import com.yc.navy.goods.enums.ResultEnum;
import com.yc.navy.goods.service.IAddressInfoService;
import com.yc.navy.goods.util.ConstantInfo;
import com.yc.navy.goods.util.StringUtil;
import com.yc.navy.goods.vo.ResultVO;

@RestController
@RequestMapping("/addr")
public class AddressInfoController {
	@Autowired
	private IAddressInfoService addressInfoService;
	
	private String  getlogin(HttpSession session) {
		Object obj = session.getAttribute(ConstantInfo.LOGINCHECK);
		if(obj == null) {
			return null;
		}
		MemberInfoDTO m = (MemberInfoDTO) obj;
		return "m_"+m.getMid();
	}
	
	@RequestMapping("/add")
	public ResultVO add(HttpSession session,AddressInfo address) {
		String mid = getlogin(session); 
		if(StringUtil.checkNull(mid)) {
			return new ResultVO(ResultEnum.NOTFOUND);
		}
		int mids = Integer.parseInt(mid.replace("m_", ""));
		address.setMid(mids);
		int result = addressInfoService.add(address);
		if(result <= 0) {
			return new ResultVO(ResultEnum.INCOMPLETEINFORMATION);
		}
		return new ResultVO(ResultEnum.SUCCESS);
	}
	
	@RequestMapping("/list")
	public ResultVO list(HttpSession session) {
		String mid = getlogin(session);
		if(StringUtil.checkNull(mid)) {
			return new ResultVO(ResultEnum.NOTFOUND);
		}
		
		int mids = Integer.parseInt(mid.replace("m_", ""));
		List<AddressInfo> list = addressInfoService.list(mids);
		if(list.isEmpty() || list.size() <= 0) {
			return new ResultVO(ResultEnum.NOTFOUND);
		}
		return new ResultVO(ResultEnum.SUCCESS,list);
	}
	/**
	 * 选定默认地址
	 * @param session
	 * @param aid
	 * @return
	 */
	@Transactional
	@RequestMapping("/update")
	public ResultVO update(HttpSession session,long aid) {
		String mid = getlogin(session); 
		
		if(StringUtil.checkNull(mid)) {
			return new ResultVO(ResultEnum.NOTFOUND);
		}
		
		if((Object)aid == null) {
			return new ResultVO(ResultEnum.INCOMPLETEINFORMATION);
		}
		
		int result = addressInfoService.updateAll(Integer.valueOf(mid));
		if(result <= 0) {
			return new ResultVO(ResultEnum.ERROR);
		}
		
		int result2 = addressInfoService.update(aid);
		if(result2 <= 0 ) {
			return new ResultVO(ResultEnum.ERROR);
		}
		return new ResultVO(ResultEnum.SUCCESS);
	}
	
	@RequestMapping("/deleteAddr")
	public ResultVO deletone(HttpSession session,long aid) {
		/**String mid = getlogin(session);
		if(StringUtil.checkNull(mid)) {
			return new ResultVO(ResultEnum.NOTLOGIN);
		}**/
		if((Object)aid == null) {
			return new ResultVO(ResultEnum.INCOMPLETEINFORMATION);
		}
		int result = addressInfoService.deleteone(aid);
		if(result <= 0) {
			return new ResultVO(ResultEnum.ERROR);
		}
		return new ResultVO(ResultEnum.SUCCESS);
	}
	
	@Transactional//在方法之前自动启动事务，在方法之后自动提交事务，在出现错误后自动回滚事务     跟update没有上传没有什么关系
	@RequestMapping("/updateone")
	public ResultVO updateone(HttpSession session,AddressInfo addr) {
		String mid = getlogin(session); 
		if(StringUtil.checkNull(mid)) {
			return new ResultVO(ResultEnum.NOTFOUND);
		}
		int result = addressInfoService.updateone(addr);
		if(result <= 0) {
			return new ResultVO(ResultEnum.ERROR);
		}
		return new ResultVO(ResultEnum.SUCCESS);
		
	}
}
